package cn.lanqiao.bigDate.service.data.impl;

import cn.lanqiao.bigDate.entity.data.DataFace;
import cn.lanqiao.bigDate.entity.data.DataUser;
import cn.lanqiao.bigDate.mapper.data.DataFaceMapper;
import cn.lanqiao.bigDate.mapper.data.DataUserMapper;
import cn.lanqiao.bigDate.service.data.DataFaceService;
import cn.lanqiao.common.utils.Base64Util;
import cn.lanqiao.common.utils.FaceUtil;
import cn.lanqiao.common.utils.MD5Util;
import com.arcsoft.face.FaceSimilar;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Marisa
 * @description 描述
 * @date 2022/4/5
 */
@Service
public class DataFaceServiceImpl
extends ServiceImpl<DataFaceMapper, DataFace>
implements DataFaceService {

    @Resource
    private DataFaceMapper dataFaceMapper;

    @Resource
    private DataUserMapper dataUserMapper;

    @Override
    public Boolean login(String imgStr) {
        String img = imgStr.substring(imgStr.indexOf(',') + 1);
        String type = "png";
        FaceUtil.init();
        QueryWrapper<DataFace> wrapper = new QueryWrapper<>();
        List<DataFace> features = dataFaceMapper.selectList(wrapper);
        FaceSimilar faceSimilar;
        for (DataFace f : features) {
            Long featureId = f.getId();
            String feature = f.getFeature();
            byte[] bytes = new byte[0];
            try {
                bytes = Base64Util.decode(feature);
            } catch (Exception e) {
                e.printStackTrace();
            }
            faceSimilar = FaceUtil.faceFeatureComparison(img, type, bytes);
            float score = faceSimilar.getScore();
            System.out.println("分数: " + score);
            if(score > 0.7f){
                QueryWrapper<DataUser> wrapper2 = new QueryWrapper<>();
                wrapper2.eq("face_id", featureId);
                DataUser dataUser = dataUserMapper.selectOne(wrapper2);
                System.out.println(dataUser);
                return true;
            }
        }
        FaceUtil.closeEngine();
        return false;
    }

    @Override
    public Boolean create(String str, String userName,String password) {
        FaceUtil.init();
//        String s = Base64Util.convertFileToBase64("D:\\img\\me.jpg");
        str = str.substring(str.indexOf(',') + 1);
        Boolean isFace = FaceUtil.faceDetection(str, "png");
        System.out.println("isFace: "+isFace);
        if(!isFace){
            return false;
        }
        byte[] bytes = FaceUtil.getFaceFeatures(str, "png");
        String encode = Base64Util.encode(bytes);

        QueryWrapper<DataUser> wrapper = new QueryWrapper<>();
        wrapper.eq("login_account", userName);
        DataUser user = dataUserMapper.selectOne(wrapper);
        String pwd = user.getPassword();
        String pwd2 = MD5Util.md5(password + user.getSalt());
        System.out.println("faceID>"+user.getFaceId()+"<");
        if(!pwd2.equals(pwd) || user.getFaceId() != null){
            return false;
        }
        DataFace dataFace = new DataFace();
        dataFace.setFeature(encode);
        // 更新特征码
        int insert = dataFaceMapper.insert(dataFace);
        System.out.println("faceID: " + dataFace.getId());
        user.setFaceId(dataFace.getId());
        // 更新绑定关系
        int i = dataUserMapper.updateById(user);
        System.out.println("i: " + i);
        System.out.println("user: " + user);
        FaceUtil.closeEngine();
        return insert == 1;
//        return false;
    }
}
